package com.leather.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.leather.common.entity.User;
import com.leather.common.result.CodeMsg;
import com.leather.common.result.Result;
import com.leather.common.toolkits.CheckKit;
import com.leather.common.toolkits.ImageKit;
import com.leather.common.toolkits.JwtKit;
import com.leather.common.toolkits.QinNiuKit;
import com.leather.user.api.UserService;
import com.leather.user.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/get/{id}")
    public User getUserById(@PathVariable("id") Integer id) {
        return userService.getById(id);
    }

    @PostMapping("/getuser")
    public Result getUser(@RequestParam("nowId") int nowId, @RequestParam("userId") Integer userId) {
        return userService.getUserInfo(nowId, userId);
    }

    @PostMapping("/touchuser")
    public Result touchUser(@RequestParam("userId") int userId, @RequestParam("touchId") Integer touchId) {
        return userService.touchUser(userId, touchId);
    }

    @PostMapping("/deletetouchuser")
    public Result deleteTouchUser(@RequestParam("userId") int userId, @RequestParam("touchId") Integer touchId) {
        return userService.deleteTouchUser(userId, touchId);
    }


    @PostMapping("/getbynumber/{number}")
    public User getUserByNumber(@PathVariable("number") String number) {
        return userService.getUserByNumber(number);
    }

    @PostMapping("/tokenlogin")
    public Result tokenLogin(@RequestHeader("token") String token) {
        String number = JwtKit.getValue(token);
        CheckKit.notEmpty(number, CodeMsg.ERROR_TOKEN);
        return Result.success(userService.getUserInfo(number));
    }

    @GetMapping("/list")
    public Object list(
            @RequestParam(required = false, defaultValue = "0") int offset,
            @RequestParam(required = false, defaultValue = "10") int limit,
            @RequestParam(required = false, defaultValue = "asc") String order,
            @RequestParam(required = false, defaultValue = "") String search
    ) {
        Page<User> page = new Page<>(offset / 10 + 1, limit);
        Map<String, Object> maps = Maps.newHashMap();
        IPage<User> users = userService.getUserListByPage(page, order, search);
        maps.put("rows", users.getRecords());
        maps.put("total", users.getTotal());
        return maps;
    }

    @GetMapping("/userinfo")
    public Result userInfo(@RequestHeader("token") String token) {
        String number = JwtKit.getValue(token);
        CheckKit.notEmpty(number, CodeMsg.ERROR_TOKEN);
        return Result.success(userService.getUserInfo(number));
    }

    @PostMapping("/login")
    public Result login(UserVo userVo) {
        return userService.login(userVo);
    }

    @PostMapping("/frozenuser")
    public Result frozenUser(@RequestParam("userId") int userId) {
        return userService.frozenUser(userId);
    }

    @PostMapping("/unfrozenuser")
    public Result unFrozenUser(@RequestParam("userId") int userId) {
        return userService.unFrozenUser(userId);
    }

    @PostMapping("/changenickname")
    public Result changeNickName(@RequestHeader("token") String token, @RequestParam("nickName") String nickName) {
        String number = JwtKit.getValue(token);
        CheckKit.notEmpty(number, CodeMsg.ERROR_TOKEN);
        return userService.changeNickName(number, nickName);
    }

    @PostMapping("/gettouched")
    public Result getTouched(@RequestParam("userId") int userId) {
        CheckKit.check(userId > 0, CodeMsg.ERROR_USER_ID);
        return userService.getTouched(userId);
    }

    //

    @PostMapping("/changeimg")
    public Result changeImg(@RequestHeader("token") String token, MultipartFile image) {
        //验证token
        String number = JwtKit.getValue(token);
        CheckKit.notEmpty(number, CodeMsg.ERROR_TOKEN);

        //获取当前用户的旧图片地址
        User user = userService.getUserByNumber(number);
        //再次验证token
        CheckKit.check(user != null, CodeMsg.ERROR_TOKEN);

        //上传图片
        String real = ImageKit.uploadImage(image,"icon");
        CheckKit.notEmpty(real, CodeMsg.QINIUYUN_ERROR);

        //删除之前的头像
        String imgUrl = user.getImgUrl();
        //删除临时文件
        if (!"icon.png".equals(imgUrl) && !QinNiuKit.deleteImage(imgUrl)) {
            return Result.error(CodeMsg.QINIUYUN_ERROR);
        }

        return userService.changeImgUrl(number, real);
    }

    @PostMapping("/changedescription")
    public Result changeDescription(@RequestHeader("token") String token, @RequestParam("description") String description) {
        String number = JwtKit.getValue(token);
        if (StringUtils.isBlank(number)) {
            return Result.error(CodeMsg.ERROR_TOKEN);
        }
        return userService.changeDescription(number, description);
    }

    @PostMapping("/getmessage")
    public Result getMessage(@RequestHeader("token") String token, @RequestParam("phone") String phone) {
        String number = JwtKit.getValue(token);
        CheckKit.notEmpty(number, CodeMsg.ERROR_TOKEN);
        return userService.getMessage(number, phone);
    }

    @PostMapping("/setphone")
    public Result setPhone(@RequestHeader("token") String token, @RequestParam("phone") String phone, @RequestParam("code") String code) {
        String number = JwtKit.getValue(token);
        CheckKit.notEmpty(number, CodeMsg.ERROR_TOKEN);
        return userService.setPhone(number, phone, code);
    }

    @PostMapping("/getstar")
    public Result getStarList(@RequestParam("userId") int userId) {
        CheckKit.check(userId > 0, CodeMsg.ERROR_USER_ID);
        return userService.getStar(userId);
    }

}
